import jkoa.core.Context;
import jkoa.core.Middleware;
import jkoa.core.Next;
import jkoa.core.Request;
import jkoa.core.Response;

public class MiddlewareExample {
    public static void main(String[] args) {
        // 创建中间件链条
        Middleware middleware1 = (ctx, next) -> {
            System.out.println("Middleware 1: Before handling request");
            next.execute(ctx);
            System.out.println("Middleware 1: After handling request");
        };

        Middleware middleware2 = (ctx, next) -> {
            System.out.println("Middleware 2: Before handling request");
            // 模拟处理请求，设置响应内容
//            ctx.response.setBody("Response from middleware 2");
            next.execute(ctx);
            System.out.println("Middleware 2: After handling request");
        };

        // 创建请求和响应对象
        Request request = new Request();
        Response response = new Response();

        Context ctx=new Context(request, response);
        // 执行中间件链条
        middleware1.handle(ctx, (ctx2) -> middleware2.handle(ctx2, (ctx4) -> {
            System.out.println("Final handling");
        }));
        
        
        
//        Next finalNext = new Next() {
//            public void handle(Request request, Response response) {
//                System.out.println("Final handling");
//            }
//        };
//        
//        
//        
//        
//      middleware1.handle(request, response,new Next() {
//          public void handle(Request request, Response response) {
//              middleware2.handle(request, response, finalNext);
//          }
//      });
  
      
      
      
//      middleware1.handle(request, response,()-> middleware2.handle(request, response, finalNext));
      
      
      
        
//        middleware1.handle(request, response, () -> middleware2.handle(request, response,
//        		
//        		
//        		
//        		));
//        
        
        
        
        
        
        
        
        
        
        
//        middleware1.handle(request, response, () -> middleware2.handle(request, response, new Next() {
//        	
//        	@Override
//			public void handle(Request request, Response response) {
//				// TODO Auto-generated method stub
//				  System.out.println("Final handling");
//			};
//        }));
        
        
//        middleware1.handle(request, response,new Next() {
//
//			@Override
//			public void handle(Request request, Response response) {
//				// TODO Auto-generated method stub
//				middleware2.handle(request, response, new Next() {
//
//					@Override
//					public void handle(Request request, Response response) {
//						// TODO Auto-generated method stub
//						  System.out.println("Final handling");
//					}});
//			}});
    }
}
